﻿using System;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Windows.Forms;
using MobileCommissionManagement.Common;

namespace MobileCommissionManagement.VodafoneControls
{
    public partial class VodafoneDealerSetupList : UserControl
    {
        private VodafoneController _vodafoneController;

        #region Properties

        public VodafoneController VodafoneController
        {
            set
            {
                _vodafoneController = value;
                _vodafoneController.DealerSetupSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public VodafoneDealerSetupList()
        {
            InitializeComponent();
            editToolStripMenuItem.Click += editToolStripMenuItem_Click;
            addToolStripMenuItem.Click += addToolStripMenuItem_Click;
            changeSituationToolStripMenuItem.Click += changeSituationToolStripMenuItem_Click;
            gridView.DoubleClick += gridView_DoubleClick;
            contextMenuStrip.Opening += contextMenuStrip_Opening;
        }

        #region Private Methods

        private void SetGridDataSource()
        {
            gridControl.DataSource = _vodafoneController.AllDealerSetups;
        }

        private ConfigurarDealer GetClone(ConfigurarDealer dealerSetup)
        {
            ConfigurarDealer document = new ConfigurarDealer();
            document.IdConfiguracao = dealerSetup.IdConfiguracao;
            document.Nome = dealerSetup.Nome;
            document.Activo = dealerSetup.Activo;
            document.Tipo = dealerSetup.Tipo;
            document.LinhaNegocio = dealerSetup.LinhaNegocio;
            EntityCollection<ConfigurarDealerColunas> dealerSetupColumns = _vodafoneController.GetDealerSetupColumns(dealerSetup.IdConfiguracao);
            if (dealerSetupColumns == null)
                return document;
            foreach (ConfigurarDealerColunas column in dealerSetupColumns)
            {
                ConfigurarDealerColunas entityDetail = new ConfigurarDealerColunas();
                entityDetail.ConfigurarDealer = document;
                entityDetail.IdConfiguracaoColuna = column.IdConfiguracaoColuna;
                entityDetail.NomeBaseDados = column.NomeBaseDados;
                entityDetail.PosicaoFicheiro = column.PosicaoFicheiro;
            }
            return document;
        }

        private static void UpdateDealerSetupColumn(ConfigurarDealerColunas column, ConfigurarDealerColunas dealerSetupColumn)
        {
            dealerSetupColumn.NomeBaseDados = column.NomeBaseDados;
            dealerSetupColumn.PosicaoFicheiro = column.PosicaoFicheiro;
        }

        private static ConfigurarDealerColunas GetDealerSetupColumn(ConfigurarDealer dealerSetup, int dealerSetupColumnId)
        {
            foreach (ConfigurarDealerColunas column in dealerSetup.ConfigurarDealerColunas)
                if (column.IdConfiguracaoColuna == dealerSetupColumnId)
                    return column;
            return null;
        }

        private static void Merge(ConfigurarDealer dealerSetup, ConfigurarDealer document)
        {
            dealerSetup.Nome = document.Nome;
            dealerSetup.Activo = document.Activo;
            dealerSetup.Tipo = document.Tipo;
            dealerSetup.LinhaNegocio = document.LinhaNegocio;
            foreach (ConfigurarDealerColunas column in document.ConfigurarDealerColunas)
            {
                ConfigurarDealerColunas dealerSetupColumn;
                if (column.IdConfiguracaoColuna < 0)
                {
                    dealerSetupColumn = new ConfigurarDealerColunas();
                    dealerSetupColumn.ConfigurarDealer = dealerSetup;
                    dealerSetupColumn.IdConfiguracaoColuna = column.IdConfiguracaoColuna;
                    UpdateDealerSetupColumn(column, dealerSetupColumn);
                    continue;
                }
                dealerSetupColumn = GetDealerSetupColumn(dealerSetup, column.IdConfiguracaoColuna);
                if (dealerSetupColumn == null)
                    dealerSetup.ConfigurarDealerColunas.Remove(dealerSetupColumn);
                else
                    UpdateDealerSetupColumn(column, dealerSetupColumn);                
            }
        }

        private void ShowDealerSetup(ConfigurarDealer dealerSetup)
        {
            ConfigurarDealer document = GetClone(dealerSetup);
            VodafoneDealerSetup window = new VodafoneDealerSetup();
            window.VodafoneController = _vodafoneController;
            window.Document = document;
            if (DialogResult.OK != window.ShowDialog())
                return;
            Merge(dealerSetup, window.Document);
            _vodafoneController.SaveDealerSetup(dealerSetup);
        }

        #endregion

        void gridView_DoubleClick(object sender, EventArgs e)
        {
            ConfigurarDealer dealerSetup = (ConfigurarDealer)gridView.GetRow(gridView.FocusedRowHandle);
            if (dealerSetup == null)
                return;
            ShowDealerSetup(dealerSetup);
        }

        void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConfigurarDealer dealerSetup = (ConfigurarDealer)gridView.GetRow(gridView.FocusedRowHandle);
            ShowDealerSetup(dealerSetup);
        }

        void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConfigurarDealer dealerSetup = new ConfigurarDealer();
            dealerSetup.IdConfiguracao = -1;
            dealerSetup.Nome = string.Empty;
            dealerSetup.Tipo = string.Empty;
            dealerSetup.LinhaNegocio = string.Empty;
            dealerSetup.Activo = true;
            ShowDealerSetup(dealerSetup);
        }

        void changeSituationToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConfigurarDealer dealerSetup = (ConfigurarDealer)gridView.GetRow(gridView.FocusedRowHandle);
            if (dealerSetup == null)
                return;
            dealerSetup.Activo = !dealerSetup.Activo;
            _vodafoneController.SaveDealerSetup(dealerSetup);
        }

        void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            ConfigurarDealer dealerSetup = (ConfigurarDealer)gridView.GetRow(gridView.FocusedRowHandle);
            editToolStripMenuItem.Enabled = dealerSetup != null;
            changeSituationToolStripMenuItem.Enabled = dealerSetup != null;
            //removeToolStripMenuItem.Enabled = false;
        } 
    }
}
